package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.ActionBean;
import com.canbot.u05.activity.presidemode.newcode.bean.DanceBean;
import com.canbot.u05.activity.presidemode.newcode.bean.HostItemBean;
import com.canbot.u05.activity.presidemode.newcode.bean.MoveBean;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.PicturnEmotionBean;
import com.canbot.u05.activity.presidemode.newcode.bean.VoiceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.ITaskItemClick;
import com.canbot.u05.control.DataUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NHostTaskAddAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private final Context mContext;
    private List<HostItemBean> mTaskList;
    private LayoutInflater layoutInflater;
    private ITaskItemClick iTaskItemClick;

    public NHostTaskAddAdapter(List<HostItemBean> taskList, Context context, ITaskItemClick iItemClick) {
        this.mTaskList = taskList;
        if (mTaskList == null) {
            mTaskList = new ArrayList<>();
        }
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.iTaskItemClick = iItemClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        switch (viewType) {
            case HostItemBean.ITEM_TYPE_DANCE:
                View views = layoutInflater.inflate(R.layout.host_task_add_dance_item, viewGroup, false);
                return new HostTaskAddDanceViewHolder(views, viewType);
            default:
                views = layoutInflater.inflate(R.layout.host_task_add_normal_item, viewGroup, false);
                return new HostTaskAddNormalViewHolder(views, viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int postion) {
        int viewType = mTaskList.get(postion).getItemType();
        switch (viewType) {
            case HostItemBean.ITEM_TYPE_DANCE:
                HostTaskAddDanceViewHolder holderDance = (HostTaskAddDanceViewHolder) viewHolder;
                holderDance.danceNameChoice.setOnClickListener(this);
                holderDance.danceNameChoice.setTag(postion);
                holderDance.danceMusicChioce.setOnClickListener(this);
                holderDance.danceMusicChioce.setTag(postion);
                setPlayOrPause(postion, holderDance.playOrPause, holderDance.itemIndex);
                setDanceData(holderDance, postion);
                handlerDanceTime(holderDance.itemTime, postion);
                break;
            case HostItemBean.ITEM_TYPE_NORMAL:
                HostTaskAddNormalViewHolder holderNormal = (HostTaskAddNormalViewHolder) viewHolder;
                holderNormal.voiceChioce.setOnClickListener(this);
                holderNormal.voiceChioce.setTag(postion);
                setVoiceChoice(postion, holderNormal.voiceChioce);
                holderNormal.actionChioce.setOnClickListener(this);
                holderNormal.actionChioce.setTag(postion);
                setActionChoice(postion, holderNormal.actionChioce);
                holderNormal.moveChioce.setOnClickListener(this);
                holderNormal.moveChioce.setTag(postion);
                setMoveChoice(postion, holderNormal.moveChioce);
                holderNormal.pictureChioce.setOnClickListener(this);
                holderNormal.pictureChioce.setTag(postion);
                setPictureOrEmotion(postion, holderNormal.pictureChioce);
                setPlayOrPause(postion, holderNormal.playOrPause, holderNormal.itemIndex);
                handlerAudioTime(holderNormal.itemTime, postion);
                break;
        }

    }

    @SuppressLint("DefaultLocale")
    private void handlerAudioTime(TextView itemTime, int postion) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        int time;
        int voiceBeanTime = hostItemBean.getVoiceBeanTime();
        int actionTime = hostItemBean.getActionTime();
        int pictureTime = hostItemBean.getPictureTime();
        int moveBeanTime = hostItemBean.getMoveBeanTime();

        if (voiceBeanTime != 0 || actionTime != 0 || moveBeanTime != 0) {
            time = Math.max(moveBeanTime, Math.max(voiceBeanTime, actionTime));
        } else {
            time = pictureTime;
        }
        itemTime.setText(DataUtil.formatTime(time));
    }

    private void handlerDanceTime(TextView itemTime, int postion) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        DanceBean danceBean = hostItemBean.getDanceBean();
        if (danceBean != null) {
            itemTime.setText(DataUtil.formatTime(danceBean.getTime()));
        } else {
            itemTime.setText(String.format("00:00"));
        }
    }

    private void setPictureOrEmotion(int postion, TextView pictureChioce) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        PicturnEmotionBean picturnEmotionBean = hostItemBean.getPicturnEmotionBean();
        if (picturnEmotionBean != null) {
            if (picturnEmotionBean.getType() == NModuleType.PICTURE) {
                String fileName = picturnEmotionBean.getFileName();
                String[] split = fileName.split("/");
                pictureChioce.setText(split[split.length - 1]);
            } else if (picturnEmotionBean.getType() == NModuleType.EMOTION) {
                pictureChioce.setText(NModuleType.getEmotionName(mContext, picturnEmotionBean.getFileName()));
            } else {
                pictureChioce.setText(mContext.getString(R.string.choice_none_picture));
            }
        } else {
            pictureChioce.setText(mContext.getString(R.string.choice_picture));
        }
    }

    private void setVoiceChoice(int postion, TextView actionChioce) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        VoiceBean voiceBean = hostItemBean.getVoiceBean();
        if (voiceBean != null) {
            switch (voiceBean.languageType) {
                case NModuleType.VOICE_NONE:
                    actionChioce.setText(mContext.getString(R.string.choice_none));
                    break;
                case NModuleType.VOICE_TTS:
                    actionChioce.setText(voiceBean.content);
                    break;
                case NModuleType.VOICE_MUSIC:
                    actionChioce.setText(voiceBean.content);
                    break;
                case NModuleType.VOICE_RECORD:
                    actionChioce.setText(voiceBean.content);
                    break;
            }
        } else {
            actionChioce.setText(mContext.getString(R.string.voice_choice));
        }
    }

    private void setMoveChoice(int postion, TextView actionChioce) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        MoveBean moveBean = hostItemBean.getMoveBean();
        if (moveBean != null) {
            String speedStr = NModuleType.getSpeedStr(mContext, moveBean.speed);
            switch (moveBean.orientation) {
                case UP:
                    actionChioce.setText(String.format("%s%s%s;%s", mContext.getString(R.string.gohead),
                            moveBean.distance, mContext.getString(R.string.mi), speedStr));
                    break;
                case DOWN:
                    actionChioce.setText(String.format("%s%s%s;%s", mContext.getString(R.string.goback),
                            moveBean.distance, mContext.getString(R.string.mi), speedStr));
                    break;
                case RIGHT:
                    actionChioce.setText(String.format("%s%s%s;%s", mContext.getString(R.string.turnright),
                            moveBean.distance, mContext.getString(R.string.angle), speedStr));
                    break;
                case LEFT:
                    actionChioce.setText(String.format("%s%s%s;%s", mContext.getString(R.string.turnleft),
                            moveBean.distance, mContext.getString(R.string.angle), speedStr));
                    break;
                case NONE:
                    actionChioce.setText(mContext.getString(R.string.choice_none_move));
                    break;
            }
        } else {
            actionChioce.setText(mContext.getString(R.string.move_choice));
        }
    }

    private void setActionChoice(int postion, TextView actionChioce) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        List<ActionBean> actionBeanList = hostItemBean.getActionBeanList();
        if (actionBeanList != null && actionBeanList.size() > 0) {
            ActionBean actionBean = actionBeanList.get(0);
            if (actionBean.actionType == NModuleType.ACTION_RANDOM) {
                actionChioce.setText(mContext.getString(R.string.action_random));
            } else if (actionBean.actionType == NModuleType.ACTION_EXIST) {
                if (actionBeanList.size() == 1) {
                    actionChioce.setText(actionBean.actionName);
                } else {
                    actionChioce.setText(String.format("%s...", actionBean.actionName));
                }
            } else {
                actionChioce.setText(mContext.getString(R.string.choice_none_action));
            }
        } else {
            actionChioce.setText(mContext.getString(R.string.choice_action));
        }
    }

    private void setPlayOrPause(int postion, Button playOrPause, TextView itemIndex) {
        playOrPause.setOnClickListener(this);
        playOrPause.setTag(postion);
        itemIndex.setText(String.valueOf(postion + 1));
        HostItemBean hostItemBean = mTaskList.get(postion);
        boolean play = hostItemBean.isPlay();
        if (play) {
            playOrPause.setSelected(true);
            playOrPause.setText(mContext.getString(R.string.stop));
        } else {
            playOrPause.setSelected(false);
            playOrPause.setText(mContext.getString(R.string.play));
        }
    }

    private void setDanceData(HostTaskAddDanceViewHolder holderDance, int postion) {
        HostItemBean hostItemBean = mTaskList.get(postion);
        DanceBean danceBean = hostItemBean.getDanceBean();
        if (danceBean != null) {
            holderDance.danceNameChoice.setText(danceBean.getContent());
            holderDance.danceMusicChioce.setText(danceBean.getPath());
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mTaskList.get(position).getItemType();
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    private static final String TAG = "NHostTaskAddAdapter";

    @Override
    public void onClick(View v) {
        int postion = (int) v.getTag();
        Log.e(TAG, "onClick: " + postion);
        switch (v.getId()) {
            case R.id.playOrPause:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onPauseOrPlay(postion);
                }
                break;
            case R.id.voice_chioce:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onVoiceChoice(postion);
                }
                break;
            case R.id.action_chioce:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onActionChoice(postion);
                }
                break;
            case R.id.move_chioce:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onMoveChoice(postion);
                }
                break;
            case R.id.picture_chioce:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onPictureChoice(postion);
                }
                break;
            case R.id.dance_action_choice:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onDanceActionChoice(postion);
                }
                break;
            case R.id.dance_music_chioce:
                if (iTaskItemClick != null) {
                    iTaskItemClick.onDanceMusicChoice(postion);
                }
                break;
        }
    }

    public class HostTaskAddNormalViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.itemIndex)
        TextView itemIndex;
        @BindView(R.id.item_time)
        TextView itemTime;
        @BindView(R.id.playOrPause)
        Button playOrPause;
        @BindView(R.id.voice_chioce)
        TextView voiceChioce;
        @BindView(R.id.action_chioce)
        TextView actionChioce;
        @BindView(R.id.move_chioce)
        TextView moveChioce;
        @BindView(R.id.picture_chioce)
        TextView pictureChioce;
        @BindView(R.id.layout_main)
        RelativeLayout relativeLayout;

        public HostTaskAddNormalViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnTouchListener((v, event) -> {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (iTaskItemClick != null) {
                        iTaskItemClick.onStartDrag(HostTaskAddNormalViewHolder.this);
                    }
                }
                return false;
            });
        }
    }

    public class HostTaskAddDanceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.itemIndex)
        TextView itemIndex;
        @BindView(R.id.dance_action_choice)
        TextView danceNameChoice;
        @BindView(R.id.dance_music_chioce)
        TextView danceMusicChioce;
        @BindView(R.id.item_time)
        TextView itemTime;
        @BindView(R.id.playOrPause)
        Button playOrPause;

        public HostTaskAddDanceViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnTouchListener((v, event) -> {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (iTaskItemClick != null) {
                        iTaskItemClick.onStartDrag(HostTaskAddDanceViewHolder.this);
                    }
                }
                return false;
            });
        }
    }
}
